import axios from "axios";
import Qs from "qs";

const items={
  namespaced: true,
  state:()=>{
    return{
      // store_id:'',
      // store_name:'',
      // item_id:'',
      // item_name:'',
      // item_price:'',
      // item_num:'',
      // item_intro:'',
      // item_picture:'',
      items_info:[],
      items_info_sup:[],
      index_page:1,
      item_current:{},
      address_list:[],
      order_info:{
        order_id:'',
        sub_order_id:'',
        item_num:'',
        address_id:''
      }

    }
  },
  mutations:{

  },
  actions:{
    getItemListCus({ state, commit, rootState }, obj){
      /**
       *  TODO:获取商品广场所有商品——买家
       */
      axios.defaults.withCredentials = true;
      console.log(Qs.stringify(obj));
      return new Promise(((resolve, reject) => {
        axios({
          method:'post',
          withCredentials: true,
          url:'/items/customer',
          data: Qs.stringify(obj),
          headers: {
            'content-type': 'application/x-www-form-urlencoded;charset=utf-8'
          }
        }).then((res)=>{
          state.items_info=res.data;
          state.items_info.splice(0,0)
          resolve();
        }).catch(e=>{
          reject(e);
        })
      }))
    },
    getItemListSup({ state, commit, rootState },obj){
      /**
       *  TODO:获取商品广场所有商品——卖家
       */
      axios.defaults.withCredentials = true;
      return new Promise(((resolve, reject) => {
        axios({
          method:'post',
          withCredentials: true,
          url: '/items/supplier',
          data: Qs.stringify(obj),
          headers: {
            'content-type': 'application/x-www-form-urlencoded;charset=utf-8'
          }
        }).then((res)=>{
          state.items_info_sup = res.data;
          state.items_info_sup.splice(0,0)
          resolve();
        }).catch(e=>{
          reject(e);
        })
      }))
    },
    getItemDetailCus({ state, commit, rootState }, obj){
      /**
       *  TODO:查询商品详情
       */
      axios.defaults.withCredentials = true;
      return new Promise(((resolve, reject) => {
        axios({
          method:'post',
          withCredentials: true,
          url: '/items/detail/customer',
          data: Qs.stringify(obj),
          headers: {
            'content-type': 'application/x-www-form-urlencoded;charset=utf-8'
          }
        }).then((res)=>{
          console.log('单个商品信息');
          state.item_current = res.data;
          console.log(state.item_current);
          resolve();
        }).catch(e=>{
          reject(e);
        })
      }))
    },
    getItemDetailSup({ state, commit, rootState }, obj){
      /**
       *  TODO:查询商品详情
       */
      axios.defaults.withCredentials = true;
      return new Promise(((resolve, reject) => {
        axios({
          method:'post',
          withCredentials: true,
          url: '/items/detail/supplier',
          data: Qs.stringify(obj),
          headers: {
            'content-type': 'application/x-www-form-urlencoded;charset=utf-8'
          }
        }).then((res)=>{
          console.log('单个商品信息');
          state.item_current = res.data;
          console.log(state.item_current);
          resolve();
        }).catch(e=>{
          reject(e);
        })
      }))
    },
    addItemToCart({ state, commit, rootState }, obj){
      /**
       *  TODO:添加至购物车
       */
      return new Promise(((resolve, reject) => {
        axios.put('/items/addtocart',
          Qs.stringify(obj),
          {
            headers: {
              'content-type': 'application/x-www-form-urlencoded;charset=utf-8'
            }
          }).then((res)=>{
          resolve();
        }).catch(e=>{
          reject(e);
        })
      }))
    },
    addItemToWish({state, commit, rootState }, obj){
      /**
       *  TODO:添加至心愿单
       */
      return new Promise(((resolve, reject) => {
        axios.put('/items/addtowishlist',
          Qs.stringify(obj),
          {
            headers: {
              'content-type': 'application/x-www-form-urlencoded;charset=utf-8'
            }
          }).then((res)=>{
          resolve();
        }).catch(e=>{
          reject(e);
        })
      }))
    },
    buyItem({ state, commit, rootState }, obj){
      /**
       *  TODO:直接购买
       */

      return new Promise(((resolve, reject) => {
        axios.post('/items/buy',
          Qs.stringify(obj),
          {
            headers: {
              'content-type': 'application/x-www-form-urlencoded;charset=utf-8'
            }
          }).then((res)=>{
            state.order_info.order_id = res.data.order_id
            state.order_info.sub_order_id = res.data.sub_order_id
          resolve()
        }).catch(e=>{
          reject(e);
        })
      }))
    },
    initOrder({ state, commit, rootState }){
      /**
       *  TODO:初始化地址
       */
      return new Promise((resolve, reject) => {
        axios.get('/user_center/address').then((res)=>{
          state.address_list=res.data;
          state.address_list.splice(0,0)
          console.log(state.address_list);
          resolve();
        }).catch(e=>{
          reject();
        })
      })
    },
    confirmOrder({ state, commit, rootState}, obj) {
      /**
       *  TODO:提交订单
       */
      return new Promise(((resolve, reject) => {
        axios.post('/items/initorder',
          Qs.stringify(obj),
          {
            headers: {
              'content-type': 'application/x-www-form-urlencoded;charset=utf-8'
            }
          }).then(()=>{
          resolve()
        }).catch(e=>{
          reject(e);
        })
      }))
    }

  }
}

export default items;
